#include"stdio.h"
#define N 9
#define NULL 0

typedef struct node
   {int data;
    struct node *lson,*rson;
   }JD;

static int a[N],b[N];
int n;
JD *building(int i,int j,int l,int u,JD *p)
{
int m,k;
if(i>j)
   p=NULL;
else
   {p=(JD*)malloc(sizeof(JD));
    p->data=a[i];
    m=l;
    k=0;
    while(m<u)
       {if(b[m]==a[i])
	   {
	    k=m;
	    m=u+1;
	   }
	else
	   m=m+1;
       }
    /*if(k==0)printf("ERROR!");
    else
       {  */
	p->lson=building(i+1,i+k-l,l,k-1,p->lson);
	p->rson=building(i+k-l+1,j,k+1,u,p->rson);
      /* }*/
   }
   return(p);
}

int n;

main()
{
JD *p;
int i;
void preorder(JD *p);
void inorder(JD *p);
void postorder(JD *p);

printf("\nPlease input the number of the tree:");
scanf("%d",&n);

printf("\nPlease input preorder:");
for(i=0;i<n;i++)
   scanf("%d",&a[i]);

printf("\nPlease input inorder:");
for(i=0;i<n;i++)
   scanf("%d",&b[i]);


/*p=(JD*)malloc(sizeof(JD));
p->data=a[0];
p->lson=NULL;
p->rson=NULL; */



p=building(1-1,n-1,1-1,n-1,p);

preorder(p);
inorder(p);
postorder(p);

}

void preorder(JD *p)
{
if(p!=NULL)
   {printf("%c\t",p->data);
    preorder(p->lson);
    preorder(p->rson);
   }
}
void inorder(JD *p)
{
if(p!=NULL)
  {
   inorder(p->lson);
   printf("%c\t",p->data);
   inorder(p->rson);
  }
}

void postorder(JD *p)
{
if(p!=NULL)
  {
   postorder(p->lson);
   postorder(p->rson);
   printf("%c\t",p->data);
  }
}
